<template>
    <div>
        <!-- 一，面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 二，卡片视图区域 -->
        <el-card>
            <!-- 1，搜索和添加区域，layout布局，行  -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList()">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="5">
                    <el-button type="primary" @click="goAddpage()">添加商品
                    </el-button>
                </el-col>
            </el-row>
            <!-- 2，用户列表区域，table -->
            <el-table :data="goodslist" border stripe style="width: 100%">
                <el-table-column type="index" label="#" width="50">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="">
                </el-table-column>
                <el-table-column prop="goods_number" label="数量" width="80">
                </el-table-column>
                <el-table-column prop="goods_price" label="价格" width="80">
                </el-table-column>
                <el-table-column prop="add_time" label="创建事件" width="150">
                    <template slot-scope="scope">
                        {{ scope.row.add_time | dataFormat }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini"
                            @click="removeById(scope.row.goods_id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 3，分页区域 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[1, 3, 5, 10]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total" background>
            </el-pagination>
        </el-card>
        <!-- 三，添加商品对话框 -->
        <el-dialog title="添加商品" :visible.sync="addGoodsDialogVisible" width="50%">
            <span>添加商品</span>
            <el-form :model="addForm" ref="addFormRef" label-width="100px">
                <el-form-item label="商品名称" prop="goods_name">
                    <el-input v-model="addForm.goods_name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addGoodsDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addGoodsDialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>
<!-- 
    商品列表组件List.vue
    1,创建路由，创建组件，面包屑，卡片，分页。时间格式化弄个全局过滤器Vue.filter('dataFormat', function (originVal) { }）。
    2,搜索功能 @click="getGoodsList()" ， @clear="getGoodsList()"
    3，删除功能。弹框确认。发送删除请求。
    4，添加功能。路由跳转到添加商品组件页面 Add.vue。
 -->
<script>
export default {
    data() {
        return {
            // 请求查询参数对象
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 3
            },
            // 请求查询数据
            goodslist: [],
            total: 0,
            //添加商品对话框，显示隐藏
            addGoodsDialogVisible: false,
            //添加商品对话框，绑定数组
            addForm: [],
        }
    },

    created() {
        this.getGoodsList()
    },

    methods: {
        // 请求查询数据列表
        async getGoodsList() {
            const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
            // console.log(res)
            if (res.meta.status !== 200) {
                return this.$message.error(res.meta.msg)
            }
            this.goodslist = res.data.goods
            this.total = res.data.total
            // console.log(this.goodslist)
            this.$message.success(res.meta.msg)
        },

        //监听 pagesize 每页条数改变事件
        handleSizeChange(neWSize) {
            // console.log(neWSize)
            this.queryInfo.pagesize = neWSize
            this.getGoodsList()
        },
        //监听 pagenum 第几页改变事件
        handleCurrentChange(newPage) {
            console.log(newPage)
            this.queryInfo.pagenum = newPage
            this.getGoodsList()
        },

        //根据id删除商品
        async removeById(id) {
            // console.log(id)
            const confirmResult = await this.$confirm(
                '此操作将永久删除该商品, 是否继续?',
                '提示',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
                // ).catch(err => {
                //     return err
                // })
            ).catch(err => err)
            // 如果用户确认删除，则返回值为字符串 confirm
            // 如果用户取消了删除，则返回值为字符串 cancel
            // console.log(confirmResult)
            if (confirmResult !== 'confirm') {
                return this.$message.info('已取消删除')
            }
            // console.log('确认了删除')
            const { data: res } = await this.$http.delete(`goods/${id}`)
            if (res.meta.status !== 200) {
                return this.$message.error(res.meta.msg)
            }
            this.$message.success(res.meta.msg)
            this.getGoodsList()
        },

        //添加商品
       goAddpage(){
           this.$router.push('/goods/add')
       },
    }
}
</script>

<style lang="less" scoped>
</style>